//
//  MainEcgCell.m
//  CareRingApp
//
//  Created by Linktop on 2024/5/8.
//

#import "MainEcgCell.h"
#import "TimeUtils.h"
#import "ConfigModel.h"
#import "UIViewController+Custom.h"

extern const CGFloat ACT_IMG_HEIGHT;
@implementation MainEcgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUi];
    }
    return self;
   
}

-(void)initUi {
    
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = self.contentView.backgroundColor;
    
    UIView *bgV = [UIView new];
    bgV.backgroundColor = ITEM_BG_COLOR;
    bgV.layer.cornerRadius = ITEM_CORNOR_RADIUS;
    bgV.layer.masksToBounds = YES;
    
    [self.contentView addSubview:bgV];
    [bgV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).inset(20);
        
    }];
    
    
    self.title = [UILabel new];
//    self.valuelabel = [UILabel new];
    self.subLbl_a = [UILabel new];
    self.subLbl_a.adjustsFontSizeToFitWidth = YES;
    
    self.subLbl_b = [UILabel new];
    self.subLbl_b.font = [UIFont systemFontOfSize:15];
    self.subLbl_b.adjustsFontSizeToFitWidth = YES;
    self.subLbl_b.numberOfLines = 2;
    
    self.timeLbl = [UILabel new];
    self.timeLbl.textAlignment = NSTextAlignmentRight;
    self.timeLbl.textColor = [UIColor lightGrayColor];
    
    self.arrowBtn = [[QMUIButton alloc]init];
    
    [self.arrowBtn addTarget:self action:@selector(arrowClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.arrowBtn setImage:[UIImage imageNamed:@"bing_arrow"] forState:UIControlStateNormal];
//    [self.arrowBtn setUserInteractionEnabled:NO];
    
    self.bottomImage = [UIImageView new];
    self.bottomImage.contentMode = UIViewContentModeScaleAspectFit;
    
    self.bottomImgInside = [UIImageView new];
    self.bottomImgInside.contentMode = UIViewContentModeScaleAspectFit;

    self.drawContent = [UIView new];
//    self.drawView.avgLineColor = UIColor.whiteColor;
    self.title.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:20];
//    self.valuelabel.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:40];//[UIFont systemFontOfSize:40];
    
//    self.realTipLbl = [UILabel new];
//    self.realTipLbl.numberOfLines = 0;
//    self.realTipLbl.adjustsFontSizeToFitWidth = YES;
   
    
    [self.contentView addSubview: self.title];
//    [self.contentView addSubview: self.valuelabel];
    [self.contentView addSubview: self.subLbl_a];
    [self.contentView addSubview: self.subLbl_b];
    [self.contentView addSubview: self.arrowBtn];
    [self.contentView addSubview:self.timeLbl];
//    [self.contentView addSubview: self.realTipLbl];
    
    UIView *bottomImgCntView = [UIView new];
    UIView *bgView = [UIView new];
    [bottomImgCntView addSubview:bgView];
    
    [bottomImgCntView addSubview:self.bottomImage];
    [bottomImgCntView addSubview:self.bottomImgInside];
    
    self.bottomImage.image = [UIImage imageNamed:@"active_hr_bg"];
    self.bottomImgInside.image = [UIImage imageNamed:@"main_ecg_cell_hr"];
    
    [self.contentView addSubview: bottomImgCntView];
    
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeartBtn:)];
//    [bottomImgCntView addGestureRecognizer:tap];
    [self.contentView addSubview: self.drawContent];

    
    [self.title mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.leading.equalTo(self.contentView.mas_leading).offset(MAIN_ACTIVE_ITEM_LEADING_MARGIN);
        make.height.equalTo(@35);
    }];
    [self.arrowBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView.mas_trailing).inset(5);
        make.top.equalTo(self.contentView.mas_top);
        make.height.equalTo(self.title.mas_height);
//        make.width.equalTo(@80);
    }];
    
    [bottomImgCntView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView);
        make.width.equalTo(bottomImgCntView.mas_height);
        make.height.equalTo(@(ACT_IMG_HEIGHT));
//        make.bottom.equalTo(self.contentView.mas_bottom).inset(25);
        make.top.equalTo(self.drawContent.mas_bottom).offset(10);
    }];
    [self.bottomImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(self.bottomImage.superview);
//        make.leading.equalTo(self.contentView);
//        make.width.equalTo(self.bottomImage.mas_height);
//        make.height.equalTo(@(ACT_IMG_HEIGHT));
//        make.bottom.equalTo(self.contentView.mas_bottom).inset(20);
    }];
    
    [self.bottomImgInside mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bottomImgInside.superview.mas_centerX);
        make.centerY.equalTo(self.bottomImgInside.superview.mas_centerY);
        make.width.equalTo(self.bottomImgInside.mas_height);
        make.height.equalTo(self.bottomImgInside.superview.mas_height).multipliedBy(0.6);
    }];
    
    [bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(bgView.superview);

    }];
    
//    [ self.valuelabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(bottomImgCntView.mas_trailing).offset(8);
//        make.top.bottom.equalTo(bottomImgCntView);
////        make.width.lessThanOrEqualTo(@50);
//    }];
    
    [self.subLbl_a mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(bottomImgCntView.mas_trailing).offset(8);
        make.top.equalTo(bottomImgCntView.mas_top);
        make.trailing.lessThanOrEqualTo(self.timeLbl.mas_leading).inset(2);

    }];
    
    [self.subLbl_b mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.subLbl_a.mas_leading);
        make.bottom.equalTo(bottomImgCntView.mas_bottom);
        make.top.equalTo(self.subLbl_a.mas_bottom);
        make.height.equalTo(self.subLbl_a.mas_height);
        make.trailing.lessThanOrEqualTo(self.timeLbl.mas_leading).inset(2);
    }];
    
    [self.timeLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.arrowBtn.mas_trailing);
        make.centerY.equalTo(bottomImgCntView.mas_centerY);
        make.width.greaterThanOrEqualTo(@(40));
    }];
    
   
    
//    [self.realTipLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.valuelabel.mas_leading);
//        make.trailing.equalTo(self.realTipLbl.superview.mas_trailing).inset(5);
//        make.centerY.equalTo(bottomImgCntView.mas_centerY);
//    }];
//    self.realTipLbl.hidden = YES;
    [self.drawContent addSubview:self.hmEcgDrawLineView];
    [self.drawContent addSubview:self.gridView];
    
    [self.drawContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.contentView);
        make.top.equalTo(self.title.mas_bottom);
        make.bottom.equalTo(self.gridView.mas_bottom).offset(10);
        
    }];
 
    
    [self.hmEcgDrawLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(self.gridView);
    }];
    
    CGFloat gridIntegetWidth =  [LTHmEcgGridView integetLengthFrom:UIScreen.mainScreen.bounds.size.width - VC_ITEM_MARGIN_HOR*2 - 10*2 ];

    [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.gridView.superview.mas_leading).offset(10);
//        make.trailing.equalTo(self.gridView.superview.mas_trailing).inset(10);
        make.width.equalTo(@(gridIntegetWidth));
        make.centerX.equalTo(self.gridView.superview.mas_centerX);
        make.top.equalTo(self.gridView.superview.mas_top).offset(10);
        make.height.equalTo(@([self.gridView lengthBySquareCount:5 * 5]));
//        make.bottom.equalTo(self.gridView.superview.mas_bottom).inset(0);
    }];
    
    
    self.title.text = _L(L_BTN_ECG);
    
}

-(void)drawWave {
    [self layoutIfNeeded];
    [self.contentView layoutIfNeeded];
    [self.hmEcgDrawLineView layoutIfNeeded];
    // 画图
    // 画图 从第 10s - 20s
    int begin = 0;
    int end = (int)self.ecgRecord.volArray.count;
    
    if (self.ecgRecord.volArray.count > 10 * 512) {
        begin = 10 * 512;
    }
    
    if (self.ecgRecord.volArray.count > 20 * 512) {
        end = 20 * 512;
    }
    NSArray<NSNumber *> *subWaves = [self.ecgRecord.volArray subarrayWithRange:NSMakeRange(begin, end)];
    self.hmEcgDrawLineView.direction = self.ecgRecord.wearHand;
    [self.hmEcgDrawLineView drawAllData:subWaves];
}

-(void)setEcgRecord:(DBEcgRecord *)ecgRecord
{
    _ecgRecord = ecgRecord;
    
    if (_ecgRecord) {
        // 时间
        NSDateFormatter *fmt = [NSDateFormatter new];
        fmt.dateFormat = @"HH:mm";
        self.timeLbl.text = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:_ecgRecord.startTs.doubleValue]];
        // 平均心率
//        self.valuelabel.text = [NSString stringWithFormat:@"%d", _ecgRecord.avgHeartRate.intValue];
        
        // 结论 + 症状
        NSString *conclusion = [DBEcgRecord titleOfConclusion:_ecgRecord.conclusion];
        
        NSString *symtoms = nil;
        if (_ecgRecord.symptomsArray.count > 0) {
            if (_ecgRecord.symptomsArray.count > 1) {
                
                symtoms = [NSString stringWithFormat:_L(L_ITEM_SYMPTOM_S), _ecgRecord.symptomsArray.count];

            } else {
                if (_ecgRecord.symptomsArray.firstObject.intValue == SYMPTOM_NONE) {
                    symtoms = _L(L_NO_SYMPTOMS);
                } else {
                    symtoms = [NSString stringWithFormat:_L(L_ITEM_SYMPTOM), _ecgRecord.symptomsArray.count];
                }
            }
            symtoms = [NSString stringWithFormat:@" (%@)", symtoms];
        }
        
        UIColor *sympColor = UIColor.redColor;
        if (_ecgRecord.symptomsArray.count == 1 &&
            _ecgRecord.symptomsArray.firstObject.intValue == SYMPTOM_NONE) {
            sympColor = UIColor.lightGrayColor;
        }
        
        // 富文本
        self.subLbl_a.attributedText = [UIViewController bigSmallAttrString:conclusion FontA:[UIFont systemFontOfSize:15] TxtColorA:UIColor.whiteColor StrB:symtoms FontB:[UIFont systemFontOfSize:15] TxtColorB: sympColor];
        
        self.subLbl_b.text = [NSString stringWithFormat:@"%@ %d %@", _L(L_AVG_HR), _ecgRecord.avgHeartRate.intValue, _L(L_UNIT_HR)];
        [self drawWave];
        return;
    }
    
    if (!_ecgRecord) {
        self.timeLbl.text = nil;
//        self.valuelabel.text = NONE_VALUE;
        self.subLbl_a.attributedText = nil;
        self.subLbl_b.text = nil;
        [self.hmEcgDrawLineView reset];
    }
    
}

-(void)arrowClick:(UIButton *)btn {

    if (self.arrowClickBLK) {
        self.arrowClickBLK(self.ecgRecord);
    }
    
    
}
-(LTHmEcgGridView *)gridView
{
    if (!_gridView) {
        _gridView = [[LTHmEcgGridView alloc]init];
    }
    return _gridView;
}

-(LTRingEcgDrawLineView *)hmEcgDrawLineView
{
    if (!_hmEcgDrawLineView) {
        LTRingEcgDrawLineView *lineView = [[LTRingEcgDrawLineView alloc]init];
        lineView.backgroundColor = UIColor.clearColor;//ECG_BACKGROUND_COLOR_DEFAULT;
        //suggest to use HORIZONTAL_SPEED_DEFAULT and VERTIAL_GAIN_DEFAULT
        
//        NSNumber *spType = [AccountCenter instance].currentAccount.otherInfo.ecgXspeedType;
//        NSNumber *ygainType = [AccountCenter instance].currentAccount.otherInfo.ecgYgainType;
//
//
//        [lineView setHorizonalSpeed:spType.unsignedIntValue VertialGain:ygainType.unsignedIntValue];
        
        lineView.ecgLineColor = BUTTON_RED;
        _hmEcgDrawLineView = lineView;
    }
    
    return _hmEcgDrawLineView;
}
@end
